<?php 
namespace Framework;
class Router{
    protected $routes = [];
    /**
     * 注册一条路由规则
     * 
     * @param string $method
     * @param string $uri
     * @param string $action
     * @return void
     */
    private function registerRoute($method,$uri,$action){
        list($controller,$controllerMethod )= explode('@',$action);
        inspectAndDie($controllerMethod);
        $this->routes[]=[
            'method'=>$method,
            'uri'=>$uri,
            'controller'=>$controller
        ];
    }
    public function addGet($uri,$controller){
        $this->registerRoute('GET',$uri,$controller);
    }
    public function addPost($uri,$controller){
        $this->registerRoute('POST',$uri,$controller);
    }
    public function addPut($uri,$controller){
        $this->registerRoute('PUT',$uri,$controller);
    }
    public function addDelete($uri,$controller){
        $this->registerRoute('DELETE',$uri,$controller);
    }
    /**
     * 执行路由
     * @param string $uri
     * @param string $method
     * @param void
     */
    public function route($uri,$method){
        foreach($this->routes as $route){
            if($route['uri']===$uri && $route['method'] === $method)
            {
                require basePath('App/' . $route['controller']);
                return;
            }
        }
        $this->error();  // 默认传递404
    }
    public function error($httpCode = 404){
        http_response_code($httpCode);
        loadView("error/{$httpCode}");
        exit;
    }
    
    
}

